/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.mda.services;

import br.ufba.lasid.base.api.IResource;
import br.ufba.lasid.jitclouds.mda.JiTContext;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author aliriosa
 */
@WebService(serviceName = "JiTFailureDetectorService")

public class JiTFailureDetectorService {

    private static final Log logger = LogFactory.getLog(JiTFailureDetectorService.class);
    
    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "register")
    public String register(@WebParam(name = "hostID") String hostID, @WebParam(name = "isLocal") boolean isLocal) {
        try {
            JiTContext.getFailureDetector().register(hostID, isLocal);
        } catch (Exception ex) {
            logger.error("Couldn't register host " + hostID, ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "buildup")
    public String buildup() {
        try {
            JiTContext.getFailureDetector().buildup();
        } catch (Exception ex) {
            logger.error("Couldn't build up.", ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "startup")
    public String startup() {
        try {
            JiTContext.getFailureDetector().startup();
        } catch (Exception ex) {
            logger.error("Couldn't start up.", ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "setPeriod")
    public String setPeriod(@WebParam(name = "period") long period) {
        try {
            JiTContext.getFailureDetector().setPeriod(period);
        } catch (Exception ex) {
            logger.error("Couldn't set period " + period, ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "setTimeout")
    public String setTimeout(@WebParam(name = "time") long time) {
        try {
            JiTContext.getFailureDetector().setTimeout(time);
        } catch (Exception ex) {
            logger.error("Couldn't set time out " + time, ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "query")
    public String query(@WebParam(name = "resourceAddress") String resourceAddress) {
        try {
            IResource.States state = JiTContext.getFailureDetector().query(resourceAddress);
            return state.toString();
        } catch (Exception ex) {
            // logger.error("Couldn't query " + resourceAddress, ex);
            return Boolean.FALSE.toString();
        }
        // return Boolean.TRUE.toString();
    }
    
}
